﻿/******************************************************************************
文件名称    :   MySqlChemistryDataProvider.cs
创建日期    :   2011/5/29 10:50:54
功能描述    :   化验检实体数据提供程序MySql实现类
修改日志    :   
    2011-12-15 10:50  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// 化验检实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlChemistryDataProvider : ChemistryDataProvider
    {
        public MySqlChemistryDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, Chemistry entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry(chemistry_blood_id, chemistry_cruor_id, chemistry_urine_id, chemistry_immunity_id, chemistry_lymphocyte_id, chemistry_biochemistry_id, chemistry_urine24h_id, chemistry_sperm_id, chemistry_pg_id, urine_drug, stone_drug, ingredient, comment) values(?chemistry_blood_id, ?chemistry_cruor_id, ?chemistry_urine_id, ?chemistry_immunity_id, ?chemistry_lymphocyte_id, ?chemistry_biochemistry_id, ?chemistry_urine24h_id, ?chemistry_sperm_id, ?chemistry_pg_id, ?urine_drug, ?stone_drug, ?ingredient, ?comment);" +
                    "select id, time_stamp from tb_chemistry where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_blood_id", DbType.Int64).Value = entity.ChemistryBloodId == null ? DBNull.Value : (object)entity.ChemistryBloodId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_cruor_id", DbType.Int64).Value = entity.ChemistryCruorId == null ? DBNull.Value : (object)entity.ChemistryCruorId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_urine_id", DbType.Int64).Value = entity.ChemistryUrineId == null ? DBNull.Value : (object)entity.ChemistryUrineId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_immunity_id", DbType.Int64).Value = entity.ChemistryImmunityId == null ? DBNull.Value : (object)entity.ChemistryImmunityId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_lymphocyte_id", DbType.Int64).Value = entity.ChemistryLymphocyteId == null ? DBNull.Value : (object)entity.ChemistryLymphocyteId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_biochemistry_id", DbType.Int64).Value = entity.ChemistryBiochemistryId == null ? DBNull.Value : (object)entity.ChemistryBiochemistryId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_urine24h_id", DbType.Int64).Value = entity.ChemistryUrine24hId == null ? DBNull.Value : (object)entity.ChemistryUrine24hId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_sperm_id", DbType.Int64).Value = entity.ChemistrySpermId == null ? DBNull.Value : (object)entity.ChemistrySpermId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_pg_id", DbType.Int64).Value = entity.ChemistryPGId == null ? DBNull.Value : (object)entity.ChemistryPGId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?urine_drug", DbType.String).Value = entity.UrineDrug == null ? DBNull.Value : (object)entity.UrineDrug;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_drug", DbType.String).Value = entity.StoneDrug == null ? DBNull.Value : (object)entity.StoneDrug;
                GenericDbHelper.AddParameter(mgr, cmd, "?ingredient", DbType.String).Value = entity.Ingredient == null ? DBNull.Value : (object)entity.Ingredient;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<Chemistry> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_chemistry(id, chemistry_blood_id, chemistry_cruor_id, chemistry_urine_id, chemistry_immunity_id, chemistry_lymphocyte_id, chemistry_biochemistry_id, chemistry_urine24h_id, chemistry_sperm_id, chemistry_pg_id, urine_drug, stone_drug, ingredient, comment, time_stamp) values(?id, ?chemistry_blood_id, ?chemistry_cruor_id, ?chemistry_urine_id, ?chemistry_immunity_id, ?chemistry_lymphocyte_id, ?chemistry_biochemistry_id, ?chemistry_urine24h_id, ?chemistry_sperm_id, ?chemistry_pg_id, ?urine_drug, ?stone_drug, ?ingredient, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_blood_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_cruor_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_urine_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_immunity_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_lymphocyte_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_biochemistry_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_urine24h_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_sperm_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_pg_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?urine_drug", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_drug", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?ingredient", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (Chemistry entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.ChemistryBloodId == null ? DBNull.Value : (object)entity.ChemistryBloodId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryCruorId == null ? DBNull.Value : (object)entity.ChemistryCruorId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryUrineId == null ? DBNull.Value : (object)entity.ChemistryUrineId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryImmunityId == null ? DBNull.Value : (object)entity.ChemistryImmunityId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryLymphocyteId == null ? DBNull.Value : (object)entity.ChemistryLymphocyteId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryBiochemistryId == null ? DBNull.Value : (object)entity.ChemistryBiochemistryId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryUrine24hId == null ? DBNull.Value : (object)entity.ChemistryUrine24hId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistrySpermId == null ? DBNull.Value : (object)entity.ChemistrySpermId.Value;
                    cmd.Parameters[i++].Value = entity.ChemistryPGId == null ? DBNull.Value : (object)entity.ChemistryPGId.Value;
                    cmd.Parameters[i++].Value = entity.UrineDrug == null ? DBNull.Value : (object)entity.UrineDrug;
                    cmd.Parameters[i++].Value = entity.StoneDrug == null ? DBNull.Value : (object)entity.StoneDrug;
                    cmd.Parameters[i++].Value = entity.Ingredient == null ? DBNull.Value : (object)entity.Ingredient;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, Chemistry entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_chemistry set chemistry_blood_id = ?chemistry_blood_id, chemistry_cruor_id = ?chemistry_cruor_id, chemistry_urine_id = ?chemistry_urine_id, chemistry_immunity_id = ?chemistry_immunity_id, chemistry_lymphocyte_id = ?chemistry_lymphocyte_id, chemistry_biochemistry_id = ?chemistry_biochemistry_id, chemistry_urine24h_id = ?chemistry_urine24h_id, chemistry_sperm_id = ?chemistry_sperm_id, chemistry_pg_id = ?chemistry_pg_id, urine_drug = ?urine_drug, stone_drug = ?stone_drug, ingredient = ?ingredient, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_chemistry where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_blood_id", DbType.Int64).Value = entity.ChemistryBloodId == null ? DBNull.Value : (object)entity.ChemistryBloodId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_cruor_id", DbType.Int64).Value = entity.ChemistryCruorId == null ? DBNull.Value : (object)entity.ChemistryCruorId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_urine_id", DbType.Int64).Value = entity.ChemistryUrineId == null ? DBNull.Value : (object)entity.ChemistryUrineId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_immunity_id", DbType.Int64).Value = entity.ChemistryImmunityId == null ? DBNull.Value : (object)entity.ChemistryImmunityId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_lymphocyte_id", DbType.Int64).Value = entity.ChemistryLymphocyteId == null ? DBNull.Value : (object)entity.ChemistryLymphocyteId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_biochemistry_id", DbType.Int64).Value = entity.ChemistryBiochemistryId == null ? DBNull.Value : (object)entity.ChemistryBiochemistryId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_urine24h_id", DbType.Int64).Value = entity.ChemistryUrine24hId == null ? DBNull.Value : (object)entity.ChemistryUrine24hId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_sperm_id", DbType.Int64).Value = entity.ChemistrySpermId == null ? DBNull.Value : (object)entity.ChemistrySpermId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?chemistry_pg_id", DbType.Int64).Value = entity.ChemistryPGId == null ? DBNull.Value : (object)entity.ChemistryPGId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?urine_drug", DbType.String).Value = entity.UrineDrug == null ? DBNull.Value : (object)entity.UrineDrug;
                GenericDbHelper.AddParameter(mgr, cmd, "?stone_drug", DbType.String).Value = entity.StoneDrug == null ? DBNull.Value : (object)entity.StoneDrug;
                GenericDbHelper.AddParameter(mgr, cmd, "?ingredient", DbType.String).Value = entity.Ingredient == null ? DBNull.Value : (object)entity.Ingredient;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_chemistry where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_chemistry {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override Chemistry Get(TransactionManager manager, long key)
        {
            Chemistry entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_chemistry where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<Chemistry> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<Chemistry> list = new List<Chemistry>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_chemistry {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_chemistry {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_chemistry {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override Chemistry Fill(IDataReader reader, Chemistry entity)
        {
            if (entity == null) entity = new Chemistry();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "chemistry_blood_id":
                        entity.ChemistryBloodId = reader[i] == DBNull.Value ? entity.ChemistryBloodId : reader.GetInt64(i);
                        break;
                    case "chemistry_cruor_id":
                        entity.ChemistryCruorId = reader[i] == DBNull.Value ? entity.ChemistryCruorId : reader.GetInt64(i);
                        break;
                    case "chemistry_urine_id":
                        entity.ChemistryUrineId = reader[i] == DBNull.Value ? entity.ChemistryUrineId : reader.GetInt64(i);
                        break;
                    case "chemistry_immunity_id":
                        entity.ChemistryImmunityId = reader[i] == DBNull.Value ? entity.ChemistryImmunityId : reader.GetInt64(i);
                        break;
                    case "chemistry_lymphocyte_id":
                        entity.ChemistryLymphocyteId = reader[i] == DBNull.Value ? entity.ChemistryLymphocyteId : reader.GetInt64(i);
                        break;
                    case "chemistry_biochemistry_id":
                        entity.ChemistryBiochemistryId = reader[i] == DBNull.Value ? entity.ChemistryBiochemistryId : reader.GetInt64(i);
                        break;
                    case "chemistry_urine24h_id":
                        entity.ChemistryUrine24hId = reader[i] == DBNull.Value ? entity.ChemistryUrine24hId : reader.GetInt64(i);
                        break;
                    case "chemistry_sperm_id":
                        entity.ChemistrySpermId = reader[i] == DBNull.Value ? entity.ChemistrySpermId : reader.GetInt64(i);
                        break;
                    case "chemistry_pg_id":
                        entity.ChemistryPGId = reader[i] == DBNull.Value ? entity.ChemistryPGId : reader.GetInt64(i);
                        break;
                    case "urine_drug":
                        entity.UrineDrug = reader[i] == DBNull.Value ? entity.UrineDrug : reader.GetString(i);
                        break;
                    case "stone_drug":
                        entity.StoneDrug = reader[i] == DBNull.Value ? entity.StoneDrug : reader.GetString(i);
                        break;
                    case "ingredient":
                        entity.Ingredient = reader[i] == DBNull.Value ? entity.Ingredient : reader.GetString(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
